﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LorraineSec.Libraries.Sensors
{
    /// <summary>
    /// A frame to hold data received from the sensor.
    /// </summary>
    public class SensorFrame
    {
        /// <summary>
        /// The time at which the data was received.
        /// </summary>
        public DateTime Timestamp { private set; get; }
        public Sensor Sender { private set; get; }
        public SensorData Data;

        public SensorFrame(Sensor send) : this(DateTime.UtcNow, send) { }

        public SensorFrame(DateTime time, Sensor send)
        {
            Sender = send;
            Timestamp = time;
        }

        public class SensorData
        {
            private byte[] byteDat;

            private SensorData(byte[] dat)
            {
                byteDat = dat;
            }
            public static implicit operator SensorData(string str)
            {
                return ASCIIEncoding.ASCII.GetBytes(str);
            }
            public static implicit operator SensorData(byte[] dat)
            {
                return new SensorData(dat);
            }
            public static explicit operator byte[](SensorData obj)
            {
                return obj.byteDat;
            }
            public static explicit operator string(SensorData obj)
            {
                return ASCIIEncoding.ASCII.GetString(obj.byteDat);
            }

            public static SensorData operator +(SensorData x, SensorData y)
            {
                byte[] newDat = new byte[x.byteDat.Length + y.byteDat.Length];
                Buffer.BlockCopy(x.byteDat, 0, newDat, 0, x.byteDat.Length);
                Buffer.BlockCopy(y.byteDat, 0, newDat, x.byteDat.Length, y.byteDat.Length);
                x.byteDat = newDat;
                return x;
            }

            public static SensorData operator +(SensorData x, byte[] y)
            {
                byte[] newDat = new byte[x.byteDat.Length + y.Length];
                Buffer.BlockCopy(x.byteDat, 0, newDat, 0, x.byteDat.Length);
                Buffer.BlockCopy(y, 0, newDat, x.byteDat.Length, y.Length);
                x.byteDat = newDat;
                return x;
            }

            public static SensorData operator +(SensorData x, string y)
            {
                return x + ASCIIEncoding.ASCII.GetBytes(y);
            }

            public byte this[int i]
            {
                get { return byteDat[i]; }
            }
        }
    }
}
